package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.client.cmn.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.BookingRule;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void saveHosp(Map<String, Object> map) {
        //借助工具类转换为json字符串
        String jsonString = JSONObject.toJSONString(map);

        //把json字符串变成对象
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);


        String hoscode = hospital.getHoscode();
        Hospital existHospital = hospitalRepository.findHospByHoscode(hoscode);
        if (existHospital != null) {
            hospital.setId(existHospital.getId());
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(existHospital.getCreateTime());
            hospital.setStatus(0);
            hospitalRepository.save(hospital);
        } else {
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(new Date());
            hospital.setStatus(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital findHospHoscode(String hoscode) {

        return hospitalRepository.findHospByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> findPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {

        //排序规则 分页
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);

        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        Example<Hospital> example = Example.of(hospital, matcher);

        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        pages.getContent().stream().forEach(item -> {
            this.packHospital(item);
        });

        return pages;
    }


    @Override
    public void updateStatus(String id, Integer status) {
       if(status.intValue() == 0 || status.intValue() == 1){
           Hospital hospital = hospitalRepository.findById(id).get();
           hospital.setStatus(status);
           hospital.setUpdateTime(new Date());
           hospitalRepository.save(hospital);
       }
    }

    @Override
    public Map<String, Object> show(String id) {
        Hospital hospital = this.packHospital(hospitalRepository.findById(id).get());

        BookingRule bookingRule = hospital.getBookingRule();
        Map<String, Object> map = new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",bookingRule);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.findHospByHoscode(hoscode);
        if(hospital != null){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosName(String hosname) {
        List<Hospital> list = hospitalRepository.findHospitalByHosnameLike(hosname);
        return list;
    }

    @Override
    public Map<String, Object> getHospDetail(String hoscode) {
        Hospital hospital = this.packHospital(hospitalRepository.findHospByHoscode(hoscode));
        Map<String, Object> result = new HashMap<>();
        result.put("hospital",hospital);
        //预约规则
        result.put("bookingRule",hospital.getBookingRule());
        return result;
    }

    //条件查询医院
    @Override
    public List<Hospital> getHospList(HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");

        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example = Example.of(hospital,matcher);
        List<Hospital> hospitalList = hospitalRepository.findAll(example, sort);
        hospitalList.stream().forEach(item->{
            this.packHospital(item);
        });
        return hospitalList;
    }


    public Hospital packHospital(Hospital hospital) {
        String hostype = hospital.getHostype();//医院等级
        String provinceCode = hospital.getProvinceCode();//省
        String cityCode = hospital.getCityCode();//市
        String districtCode = hospital.getDistrictCode();//区

        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hostype);
        String provinceString = dictFeignClient.getName(provinceCode);
        String cityString = dictFeignClient.getName(cityCode);
        String districtString = dictFeignClient.getName(districtCode);

        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());

        return hospital;
    }
}
